#define _CRT_SECURE_NO_WARNINGS 1
/*class Solution {
public:
    int sqr(int x) { return x * x; }
    bool isHappy(int n) {
        vector<int> v;
        int sum = 0;
        while (sum != 1) {

            sum = 0;
            while (n) {
                sum += sqr(n % 10);
                n /= 10;
            }
            if(find(v.begin(),v.end(),sum)!=v.end())
            {
              return false;
            }
            v.push_back(sum);
             n=sum;
        }
        return true;
    }
};*/
class Solution {
public:
    int sqr(int x) { return x * x; }
    int sub(int n) {
        int sum = 0;
        while (n) {
            sum += sqr(n % 10);
            n /= 10;
        }
        return sum;
    }
    bool isHappy(int n) {
        int slow = n, fast = sub(n);
        while (slow != fast)
        {
            fast = sub(sub(fast));
            slow = sub(slow);
        }
        if (slow == 1)
        {
            return true;
        }
        return false;
    }
};